package com.hiray.demos.visualEffect

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import androidx.core.view.ViewCompat
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import android.view.ViewTreeObserver
import com.hiray.demos.R
import com.hiray.demos.event.HideSearchEvent
import com.hiray.demos.event.RevealSearchEvent
import kotlinx.android.synthetic.main.activity_search_ele.*
import org.greenrobot.eventbus.EventBus

class SearchEleActivity : AppCompatActivity() {

    val prevColor = Color.parseColor("#ffffff")
    val searchColor = Color.parseColor("#f4f4f4")

    val duration = 300L
    val TAG = "SearchEleActivity"
    private var lastAnimateValue = 0f
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_ele)
        init()
    }

    private fun init() {
        search_seller.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                search_seller.viewTreeObserver.removeOnGlobalLayoutListener(this)
                val translateY = getTranslateY()
                val textTranslation = getHintTextTranslation()
                search_seller.translationY = translateY
                arrow.translationY = translateY
                search_text.translationY = translateY
                hint_text.translationX = textTranslation[0]
                hint_text.translationY = textTranslation[1]
                //隐藏上一个页面的搜索框
                enter()
                //要在此页的搜索框刚好显示的时候让剩一个页面的搜索隐藏，怎么检测 search_seller 刚好已经显示呢？
                window.decorView.postDelayed({
                    EventBus.getDefault().post(HideSearchEvent())
                },50)
            }

        })

    }


    var textTranslation = FloatArray(2)
    private fun getHintTextTranslation(): FloatArray {
        if (textTranslation[0] != 0f)
            return textTranslation
        val x = intent.getIntExtra("textCoordinateX", 0)
        val y = intent.getIntExtra("textCoordinateY", 0)
        val loc = IntArray(2)
        hint_text.getLocationOnScreen(loc)
        textTranslation[0] = (x - loc[0]).toFloat()
        textTranslation[1] = (y - loc[1]).toFloat()
        return textTranslation;
    }


    var sent = false
    /**
     * 入场效果
     */
    private fun enter() {
        //arrow 、searchText
        var alphaAnimator = createAnimator(0f, 1f, duration)
        alphaAnimator.addUpdateListener {
            var alpha = it.animatedValue as Float
            arrow.alpha = alpha
            search_text.alpha = alpha
            page_space.alpha = alpha
        }

        var colorAnimator = ValueAnimator.ofInt(prevColor, searchColor)
        colorAnimator.setEvaluator(ArgbEvaluator())
        colorAnimator.addUpdateListener {
            ViewCompat.setBackgroundTintList(search_seller, ColorStateList.valueOf(it.animatedValue as Int))
        }

        var scaleAnimator = createAnimator(1f, 0.8f, duration)
        scaleAnimator.addUpdateListener {
            search_seller.scaleX = it.animatedValue as Float
        }

        var to = getTranslateY()

        var translationAnimator = createAnimator(to, 0f, duration)
        translationAnimator.addUpdateListener {
            var currValue = it.animatedValue as Float
            lastAnimateValue = currValue
            search_seller.translationY = currValue
            arrow.translationY = currValue
            search_text.translationY = currValue
        }

        var hintTextTranslation = getHintTextTranslation()
        //加上由于搜索框 X方向上的scale导致的对齐误差
        var scaleDelta = search_seller.width * 0.2f / 2
        var hintTextTranslationXAnimator = createAnimator(hintTextTranslation[0], scaleDelta, duration)
        hintTextTranslationXAnimator.addUpdateListener {
            hint_text.translationX = it.animatedValue as Float
        }

        var hintTextTranslationYAnimator = createAnimator(hintTextTranslation[1], 0f, duration)
        hintTextTranslationYAnimator.addUpdateListener {
            hint_text.translationY = it.animatedValue as Float
        }
        scaleAnimator.start()
        translationAnimator.start()
        alphaAnimator.start()
        colorAnimator.start()
        hintTextTranslationXAnimator.start()
        hintTextTranslationYAnimator.start()
    }

    /**
     * 退出效果
     */
    private fun exit() {
        //arrow 、searchText
        var alphaAnimator = createAnimator(1f, 0f, duration)
        alphaAnimator.addUpdateListener {
            var alpha = it.animatedValue as Float
            arrow.alpha = alpha
            search_text.alpha = alpha
            page_space.alpha = alpha
        }

        var colorAnimator = ValueAnimator.ofInt(searchColor, prevColor)
        colorAnimator.setEvaluator(ArgbEvaluator())
        colorAnimator.addUpdateListener {
            ViewCompat.setBackgroundTintList(search_seller, ColorStateList.valueOf(it.animatedValue as Int))
        }

        var scaleAnimator = createAnimator(0.8f, 1f, duration)
        scaleAnimator.addUpdateListener {
            search_seller.scaleX = it.animatedValue as Float
        }

        var to = getTranslateY().toFloat()
        var translationAnimator = createAnimator(0f, to, duration)
        translationAnimator.addUpdateListener {
            var currValue = it.animatedValue as Float
            lastAnimateValue = currValue
            search_seller.translationY = currValue
            arrow.translationY = currValue
            search_text.translationY = currValue
        }

        alphaAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                EventBus.getDefault().post(RevealSearchEvent())
                finish()
                overridePendingTransition(0, 0)
            }
        })

        var hintTextTranslation = getHintTextTranslation()
        //加上由于搜索框 X方向上的scale导致的对齐误差
        var scaleDelta = search_seller.width * 0.2f / 2
        var hintTextTranslationXAnimator = createAnimator(scaleDelta, hintTextTranslation[0], duration)
        hintTextTranslationXAnimator.addUpdateListener {
            hint_text.translationX = it.animatedValue as Float
        }
        var hintTextTranslationYAnimator = createAnimator(0f, hintTextTranslation[1], duration)
        hintTextTranslationYAnimator.addUpdateListener {
            hint_text.translationY = it.animatedValue as Float
        }
        scaleAnimator.start()
        translationAnimator.start()
        alphaAnimator.start()
        hintTextTranslationXAnimator.start()
        hintTextTranslationYAnimator.start()
    }

    var translationY = 0
    /**
     * 获取当前搜索框 需要移动到 上个页面搜索框位置的偏移量
     */
    private fun getTranslateY(): Float {
        var prevY = intent.getIntExtra("coordinateY", 0)
        var loc = IntArray(2)
        search_seller.getLocationOnScreen(loc)
        if (translationY == 0)
            translationY = prevY - loc[1]
        return translationY.toFloat()
    }

    override fun onBackPressed() {
//        super.onBackPressed()
        exit()
    }

    private fun createAnimator(from: Float, to: Float, dur: Long): ValueAnimator {
        return ValueAnimator.ofFloat(from, to)
                .setDuration(dur)
    }

}
